package tech.aistar.controller.order;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import tech.aistar.entity.*;
import tech.aistar.service.center.IUserInfoService;
import tech.aistar.service.demand.IDemandService;
import tech.aistar.service.order.IDemandOrderService;
import tech.aistar.service.order.IServiceOrderService;
import tech.aistar.util.DateUtileDemo;
import tech.aistar.vo.DemandOrderVo;
import tech.aistar.vo.ServiceOrderVo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * Created by 23827 on 2019/9/30.
 */
@Controller
@RequestMapping("/service")
public class ServiceController {

    @Autowired
    private IDemandOrderService demandOrderService;

    @Autowired
    private IServiceOrderService iServiceOrderService;

    //作为服务者 - 报名需求和发布服务
    //报名的需求
    //展示报名的需求订单,并且分不同状态展示

    @RequestMapping("/enrolment")
    public String demand(Model model,Integer uid,HttpSession session){

        List<DemandOrderVo> demandOrderVoList = demandOrderService.demandOrderVo();
        Iterator<DemandOrderVo> iter = demandOrderVoList.iterator();
        while(iter.hasNext()){
            DemandOrderVo demandOrderVo = iter.next();
            Date time = demandOrderVo.getCommitTime();
            String dtime = DateUtileDemo.format("yyyy-MM-dd HH:mm:ss",time);
            session.setAttribute("dtime",dtime);
        }
        model.addAttribute("demandOrderVoList",demandOrderVoList);

        return "order/enrolmentDemandOrder";
    }


    //取消报名的需求订单
    @RequestMapping("/del")
    public String del(Model model,Integer id){
        DemandOrder demandOrder = demandOrderService.delById(id);
        model.addAttribute("demandOrder",demandOrder);
        List<DemandOrderVo> demandOrderVoList = demandOrderService.demandOrderVo();
        model.addAttribute("demandOrderVoList",demandOrderVoList);
        return "order/enrolmentDemandOrder";
    }

    //发布服务
    @RequestMapping("/release")
    public String list(HttpSession session){
//        List<ServiceOrderVo> serviceOrderVoList = iServiceOrderService.serviceOrderVo(id);
//        model.addAttribute("serviceOrderVoList",serviceOrderVoList);
        Integer uid = (Integer) session.getAttribute("userId");
        List<ServiceOrderVo> serviceOrderVoList = iServiceOrderService.serviceOrderVo(uid);
        Iterator<ServiceOrderVo> iter = serviceOrderVoList.iterator();
        while(iter.hasNext()){
            ServiceOrderVo serviceOrderVo =  iter.next();
            Date time = serviceOrderVo.getCommitTime();
            String atime = DateUtileDemo.format("yyyy-MM-dd HH:mm:ss",time);
            session.setAttribute("atime",atime);
        }
        session.setAttribute("serviceOrderVoList",serviceOrderVoList);
        return "order/releaseServiceOrder";
    }

}
